class List {

  constructor () {
    // 列表元素个数
    this.listSize = 0
    // 列表当前位置
    this.pos = 0
    // 集合
    this.dataStore = []
  }

  _findIndex (element) {
    return this.dataStore.findIndex(val => element === val)
  }

  /**
   * @param element
   */
  append (element) {
    this.dataStore[this.listSize++] = element
  }

  /**
   * @param element
   * @returns {boolean}
   */
  remove (element) {
    let idx = this._findIndex(element)
    if (idx > -1) {
      this.dataStore.splice(idx, 1)
      --this.listSize
      return true
    }
    return false
  }

  /**
   * @param element 插入的元素
   * @param after   after元素后插入
   * @returns {boolean}
   */
  insert (element, after) {
    const pos = this._findIndex(after)
    if (pos > -1) {
      this.dataStore.splice(pos + 1, 0, element)
      ++this.listSize
      return true
    }
    return false
  }

  /**
   * 移动至开始位置
   */
  front () {this.pos = 0}

  /**
   * 移动至末尾
   */
  end () {this.pos = this.listSize - 1}

  /**
   * 向前移动一位
   */
  prev () {if (this.pos > 0) --this.pos}

  /**
   * 向后移动一位
   */
  next () {if (this.pos < this.listSize - 1) ++this.pos}

  /**
   * 移动到指定位置
   * @param position
   */
  moveTo (position) {this.pos = position}

  /**
   * 返回当前位置
   */
  currPos () {return this.pos}

  /**
   * 返回当前位置对象
   * @returns {*}
   */
  getElement () {return this.dataStore[this.pos]}

  clear () {
    delete this.dataStore
    this.dataStore = []
    this.listSize = this.pos = 0
  }

  /**
   * @param element
   * @returns {boolean}
   */
  contains (element) {
    return (this._findIndex(element) > -1) ? true : false
  }

  /**
   * @returns {number}
   */
  length () {
    return this.listSize
  }

  /**
   * @returns {Array}
   */
  toString () {
    return this.dataStore
  }

}

function print (ele) {
  console.log(ele)
}

var names = new List()
names.append('Clayton')
names.append('Raymond')
names.append('Cynthia')
names.append('Jennifer')
names.append('Bryan')
names.append('Danny')

names.front()
print(names.getElement()) // 显示 Clayton

names.next()
print(names.getElement()) // 显示 Raymond

names.next()
names.next()
names.prev()
print(names.getElement()) // 显示 Cynthia

names.clear()
print(names.length())
